package com.cheche.dn.common.base;

import com.cheche.dn.common.listener.IdListener;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonProperty;

import javax.persistence.EntityListeners;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;
import javax.validation.groups.Default;
import java.io.Serializable;

/**
 * Entity - ID
 * Created by cheshun on 15/8/22.
 */
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE, getterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE, isGetterVisibility = JsonAutoDetect.Visibility.NONE, creatorVisibility = JsonAutoDetect.Visibility.NONE)
@EntityListeners(IdListener.class)
@MappedSuperclass
public abstract class IdEntity implements Serializable{
    private static final long serialVersionUID = -8320203946162956708L;

    /** "ID"属性名称 */
    public static final String ID_PROPERTY_NAME = "id";

    /** ID */
    private Long id;
    /** 字符串ID */
    private String _id;

    /**
     * 保存验证组
     */
    public interface Save extends Default {}

    /**
     * 更新验证组
     */
    public interface Update extends Default {}

    /**
     * 获取ID
     *
     * @return ID
     */
    @JsonProperty
    @Id
    public Long getId() {
        return id;
    }

    /**
     * 设置ID
     *
     * @param id
     *            ID
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * 获取字符串ID
     *
     * @return _ID
     */
    @JsonProperty
    @Transient
    public String get_id() {
        return _id;
    }

    /**
     * 设置字符串ID
     *
     * @param _id
     *            ID
     */
    public void set_id(String _id) {
        this._id = _id;
    }

    /**
     * 重写equals方法
     *
     * @param obj
     *            对象
     * @return 是否相等
     */
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (this == obj) {
            return true;
        }
        if (!IdEntity.class.isAssignableFrom(obj.getClass())) {
            return false;
        }
        IdEntity other = (IdEntity) obj;
        return getId() != null && getId().equals(other.getId());
    }

    /**
     * 重写hashCode方法
     *
     * @return hashCode
     */
    @Override
    public int hashCode() {
        int hashCode = 17;
        hashCode += null == getId() ? 0 : getId().hashCode() * 31;
        return hashCode;
    }
}
